/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning Asiabarometer

--------------------------------------------------------------------------------*/

*Asiabarometer 1

clear
import spss using "$data_dir/raw/Asia Barometer/W1 Merged Data/Wave.1_Data/Merge/Wave1_20170906.sav"

rename country country_ab
decode country_ab, gen(country)
replace country = "China" if country=="Mainland China"
replace country = "Taiwan, Province of China" if country=="Taiwan"
replace country = "Korea, Republic of" if country=="Korea"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Asiabarometer"
gen wave = 1

gen weight = w_all

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q008==3|q008==4 
replace trust_govt = 0 if q008==1|q008==2

gen trust_justice = 1 if q007==3|q007==4
replace trust_justice = 0 if q007==1|q007==2

gen trust_police = 1 if q013==3|q013==4
replace trust_police = 0 if q013==1|q013==2

gen trust_civil_service = 1 if q011==3|q011==4
replace trust_civil_service = 0 if q011==1|q011==2

gen trust_local_govt = 1 if q014==3|q014==4
replace trust_local_govt = 0 if q014==1|q014==2

gen trust_parties = 1 if q009==3|q009==4
replace trust_parties = 0 if q009==1|q009==2

gen trust_parliament = 1 if q010==3|q010==4
replace trust_parties = 0 if q010==1|q010==2

gen trust_others = 1 if q024==2
replace trust_others = 0 if q024==1

gen trust_military = 1 if q012==3|q012==4
replace trust_military = 0 if q012==1|q012==2

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = yrsurvey
clonevar age = se003a

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if se002==1
replace sex = 2 if se002==2
replace sex = -99 if se002==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(se006, 11)
replace religion = 2 if inlist(se006, 5)
replace religion = 4 if inlist(se006, 8, 9)
replace religion = 5 if inlist(se006, 7)
replace religion = 6 if inlist(se006, 1, 2, 3, 4, 6, 10)
replace religion = 7 if inlist(se006, 6)
replace religion = -99 if se006==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if se005==2|se005==3
replace edu = 2 if se005==4|se005==5|se005==6|se005==7
replace edu = 3 if se005==8|se005==9|se005==10
replace edu = 4 if se005==1
replace edu = -99 if se005==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if se012a==1
replace occupation = 2 if se012a==2
replace occupation = 3 if se012a==3
replace occupation = 4 if se012a==4
replace occupation = 5 if se012a==5
replace occupation = 6 if se012a==6
replace occupation = 7 if se012a==7
replace occupation = -99 if se012a==8|se012a==9|se012a==10|se012a==97|se012a==98|se012a==9|se012a==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if occupation!=-99
replace employment_status = 2 if se012a==8
replace employment_status = -99 if occupation==-99

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = .
replace income = 1 if se009==1
replace income = 3 if se009==2
replace income = 5 if se009==3
replace income = 7 if se009==4
replace income = 9 if se009==5
replace income = -99 if se009==.

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if se004a==1
replace marital = 2 if se004a==2
replace marital = -99 if se004a==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if level3==1
replace urban = 2 if level3==2
replace urban = -99 if level3==3|level3==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/asia1_clean.dta", replace

***************************************************************************************************************

*Asiabarometer 2

clear
import spss using "$data_dir/raw/Asia Barometer/W2 Merged Data/2w-3rd_release_all/merge/Wave2_20170724.sav"

rename country country_ab
decode country_ab, gen(country)
replace country = "China" if country=="Mainland China"
replace country = "Taiwan, Province of China" if country=="Taiwan"
replace country = "Korea, Republic of" if country=="Korea"
replace country = "Viet Nam" if country=="Vietnam"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Asiabarometer"
gen wave = 2

gen weight = w_all

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q9==3|q9==4 
replace trust_govt = 0 if q9==1|q9==2

gen trust_justice = 1 if q8==3|q8==4
replace trust_justice = 0 if q8==1|q8==2

gen trust_police = 1 if q14==3|q14==4
replace trust_police = 0 if q14==1|q14==2

gen trust_civil_service = 1 if q12==3|q12==4
replace trust_civil_service = 0 if q12==1|q12==2

gen trust_local_govt = 1 if q15==3|q15==4
replace trust_local_govt = 0 if q15==1|q15==2

gen trust_parties = 1 if q10==3|q10==4
replace trust_parties = 0 if q10==1|q10==2

gen trust_parliament = 1 if q11==3|q11==4
replace trust_parties = 0 if q11==1|q11==2

gen trust_others = 1 if q23==2
replace trust_others = 0 if q23==1

gen trust_military = 1 if q13==3|q13==4
replace trust_military = 0 if q13==1|q13==2

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = ir9_3
replace survey_year = 2005 if survey_year==. //assume its 2005 based on online codebook
clonevar age = se3a
replace age = . if age==99

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if se2==1
replace sex = 2 if se2==2
replace sex = -99 if se2==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(se6, 90)
replace religion = 2 if inlist(se6, 60)
replace religion = 3 if inlist(se6, 30)
replace religion = 4 if inlist(se6, 10, 20, 21, 22, 23, 24, 25, 26, 27, 81, 82, 84)
replace religion = 5 if inlist(se6, 40, 41, 42)
replace religion = 7 if inlist(se6, 50)
replace religion = -99 if inlist(se6, 98, 99)
replace religion = 6 if religion==. & se6!=.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if se5==2|se5==3
replace edu = 2 if se5==4|se5==5|se5==6|se5==7
replace edu = 3 if se5==8|se5==9|se5==10
replace edu = 4 if se5==1
replace edu = -99 if se5==.|se5==99|se5==1

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if inlist(se12a, 1, 2, 4, 80)
replace employment_status = 2 if inlist(se12a, 5, 6, 7)
replace employment_status = -99 if inlist(se12a, 3, 8, 9, 10)

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = .
replace income = 1 if se9==1
replace income = 3 if se9==2
replace income = 5 if se9==3
replace income = 7 if se9==4
replace income = 9 if se9==5
replace income = -99 if inlist(se9, 97, 98, 99)

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if inlist(se4, 2, 3)
replace marital = 2 if inlist(se4, 1, 4, 5, 6)
replace marital = -99 if inlist(se4, 9)
replace marital = -99 if se4==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if level3==1
replace urban = 2 if level3==2
replace urban = -99 if level3==3|level3==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/asia2_clean.dta", replace

***************************************************************************************************************

*Asiabarometer 3

clear
import spss using "$data_dir/raw/Asia Barometer/ABS3w.0.Merged/ABS3 merge20210506.sav"

rename country country_ab
decode country_ab, gen(country)
replace country = "China" if country=="Mainland China"
replace country = "Taiwan, Province of China" if country=="Taiwan"
replace country = "Korea, Republic of" if country=="Korea"
replace country = "Viet Nam" if country=="Vietnam"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Asiabarometer"
gen wave = 3

gen weight = couweight

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q9==1|q9==2
replace trust_govt = 0 if q9==3|q9==4

gen trust_justice = 1 if q8==1|q8==2
replace trust_justice = 0 if q8==3|q8==4

gen trust_police = 1 if q14==1|q14==2
replace trust_police = 0 if q14==3|q14==4

gen trust_civil_service = 1 if q12==1|q12==2
replace trust_civil_service = 0 if q12==3|q12==4

gen trust_local_govt = 1 if q15==1|q15==2
replace trust_local_govt = 0 if q15==3|q15==4

gen trust_parties = 1 if q10==1|q10==2
replace trust_parties = 0 if q10==3|q10==4

gen trust_parliament = 1 if q11==1|q11==2
replace trust_parties = 0 if q11==3|q11==4

gen trust_others = 1 if q23==1
replace trust_others = 0 if q23==2

gen trust_military = 1 if q13==1|q13==2
replace trust_military = 0 if q13==3|q13==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = yofd(dofc(ir9))
replace survey_year = 2010 if survey_year==1582|survey_year==1583|survey_year==2077 //assume its 2010 based on online codebook

clonevar age = se3a
replace age = . if age==-1

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if se2==1
replace sex = 2 if se2==2
replace sex = -99 if se2==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(se6, 90)
replace religion = 2 if inlist(se6, 60)
replace religion = 3 if inlist(se6, 30)
replace religion = 4 if inlist(se6, 10, 20, 24, 26, 28, 29, 74, 82, 88)
replace religion = 5 if inlist(se6, 40, 41, 42)
replace religion = 7 if inlist(se6, 50)
replace religion = -99 if inlist(se6, 98, 99, -1)
replace religion = 6 if religion==. & se6!=.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if se5==2|se5==3
replace edu = 2 if se5==4|se5==5|se5==6|se5==7
replace edu = 3 if se5==8|se5==9|se5==10
replace edu = 4 if se5==1
replace edu = -99 if se5==.|se5==99|se5==-1

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if se9==1
replace employment_status = 2 if se9==2
replace employment_status = -99 if se9==-1|se9==9|se9==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = .
replace income = 1 if se13==1
replace income = 3 if se13==2
replace income = 5 if se13==3
replace income = 7 if se13==4
replace income = 9 if se13==5
replace income = -99 if inlist(se13, -1, 8, 9)

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if inlist(se4, 2, 3)
replace marital = 2 if inlist(se4, 1, 4, 5, 6)
replace marital = -99 if inlist(se4, 9)
replace marital = -99 if se4==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if level3==1
replace urban = 2 if level3==2
replace urban = -99 if level3==3|level3==.|level3==-1

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/asia3_clean.dta", replace

***************************************************************************************************************

*Asiabarometer 4

clear
use "$data_dir/raw/Asia Barometer/W4 Merged Data/W4_v15_merged20181211_release.dta"

rename country country_ab
decode country_ab, gen(country)
replace country = "China" if country=="Mainland China"
replace country = "Taiwan, Province of China" if country=="Taiwan"
replace country = "Korea, Republic of" if country=="Korea"
replace country = "Viet Nam" if country=="Vietnam"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Asiabarometer"
gen wave = 4

gen weight = w_cross

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q9==1|q9==2
replace trust_govt = 0 if q9==3|q9==4

gen trust_justice = 1 if q8==1|q8==2
replace trust_justice = 0 if q8==3|q8==4

gen trust_police = 1 if q14==1|q14==2
replace trust_police = 0 if q14==3|q14==4

gen trust_civil_service = 1 if q12==1|q12==2
replace trust_civil_service = 0 if q12==3|q12==4

gen trust_local_govt = 1 if q15==1|q15==2
replace trust_local_govt = 0 if q15==3|q15==4

gen trust_parties = 1 if q10==1|q10==2
replace trust_parties = 0 if q10==3|q10==4

gen trust_parliament = 1 if q11==1|q11==2
replace trust_parties = 0 if q11==3|q11==4

gen trust_others = 1 if q23==1
replace trust_others = 0 if q23==2

gen trust_military = 1 if q13==1|q13==2
replace trust_military = 0 if q13==3|q13==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = year
clonevar age = se3_2
replace age = . if age==-1

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if se2==1
replace sex = 2 if se2==2
replace sex = -99 if se2==.|se2==-1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(se6, 90)
replace religion = 2 if inlist(se6, 60)
replace religion = 3 if inlist(se6, 30)
replace religion = 4 if inlist(se6, 10, 20, 24, 72, 73, 74)
replace religion = 5 if inlist(se6, 40, 42)
replace religion = 7 if inlist(se6, 50)
replace religion = -99 if inlist(se6, 98, 99, -1)
replace religion = 6 if religion==. & se6!=.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if se5==2|se5==3
replace edu = 2 if se5==4|se5==5|se5==6|se5==7
replace edu = 3 if se5==8|se5==9|se5==10
replace edu = 4 if se5==1
replace edu = -99 if se5==.|se5==99|se5==-1|se5==98|se5==11

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

tostring se9c, gen(isco)
replace isco = "" if isco=="-1"|isco=="9990"|isco=="9998"|isco=="9999"
gen occupation = regexs(0) if regex(isco, "[0-9]")
replace occupation = "-99" if occupation==""
destring occupation, replace

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if se9==1
replace employment_status = 2 if se9==2
replace employment_status = -99 if se9==-1|se9==9|se9==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = .
replace income = 1 if se14==1
replace income = 3 if se14==2
replace income = 5 if se14==3
replace income = 7 if se14==4
replace income = 9 if se14==5
replace income = -99 if inlist(se14, -1, 8, 9)

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if inlist(se4, 2, 3)
replace marital = 2 if inlist(se4, 1, 4, 5, 6)
replace marital = -99 if inlist(se4, 9)
replace marital = -99 if se4==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if level==1
replace urban = 2 if level==2
replace urban = -99 if level==3|level==.|level==-1

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/asia4_clean.dta", replace

***************************************************************************************************************

*Asiabarometer 5

///////////////////////////////////
//INDONESIA
clear
use "$data_dir/raw/Asia Barometer/W5 9. Indonesia/W5_Indonesia_merged_core_20220905_released.dta"

rename V_1	Q1
rename V_2	Q2
rename V_3	Q3
rename V_4	Q4
rename V_5	Q5
rename V_6	Q6
rename V_7	Q7
rename V_8	Q8
rename V_9	Q9
rename V_10	Q10
rename V_11	Q11
rename V_12	Q12
rename V_13	Q13
rename V_14	Q14
rename V_15	Q15
rename V_16	Q16
rename V_17	Q17
rename V_18A	Q18A
rename V_19A	Q19A
rename V_20A	Q20A
rename V_21A	Q21A
rename V_22	Q22
rename V_23	Q23
rename V_24	Q24
rename V_25	Q25
rename V_26	Q26
rename V_27	Q27
rename V_28	Q28
rename V_29	Q29
rename V_30	Q30
rename V_31	Q31
rename V_32	Q32
rename V_33	Q33
rename V_34	Q34
rename V_34_A	Q34_A
rename V_35	Q35
rename V_36	Q36
rename V_37	Q37
rename V_38	Q38
rename V_39	Q39
rename V_40	Q40
rename V_41	Q41
rename V_42	Q42
rename V_43	Q43
rename V_44	Q44
rename V_45	Q45
rename V_46	Q46
rename V_47	Q47
rename V_48	Q48
rename V_49	Q49
rename V_50	Q50
rename V_51_A	Q51_A
rename V_51_B	Q51_B
rename V_51_C	Q51_C
rename V_51_D	Q51_D
rename V_52	Q52
rename V_53	Q53
rename V_54	Q54
rename V_55	Q55
rename V_56	Q56
rename V_57	Q57
rename V_58	Q58
rename V_59	Q59
rename V_60	Q60
rename V_61	Q61
rename V_62	Q62
rename V_63	Q63
rename V_64	Q64
rename V_65	Q65
rename V_66	Q66
rename V_67	Q67
rename V_68	Q68
rename V_69	Q69
rename V_70	Q70
rename V_71	Q71
rename V_72	Q72
rename V_73	Q73
rename V_74	Q74
rename V_75	Q75
rename V_76	Q76
rename V_77	Q77
rename V_78	Q78
rename V_79	Q79
rename V_80	Q80
rename V_81	Q81
rename V_82	Q82
rename V_82A	Q82A
rename V_83	Q_83
rename V_83_A	Q83_A
rename V_84	Q_84
rename V_84_A	Q84_A
rename V_85	Q_85
rename V_85_A	Q85_A
rename V_86	Q86
rename V_87	Q87
rename V_88	Q88
rename V_89	Q89
rename V_90	Q90
rename V_91	Q91
rename V_92	Q92
rename V_93	Q93
rename V_94	Q94
rename V_95	Q95
rename V_96	Q96
rename V_97	Q97
rename V_98	Q98
rename V_99	Q99
rename V_100	Q100
rename V_101	Q101
rename V_102	Q102
rename V_103	Q103
rename V_104	Q104
rename V_105	Q105
rename V_106_A_2	Q106_A_2
rename V_106_B_2	Q106_B_2
rename V_106_C_2	Q106_C_2
rename V_107	Q107
rename V_108	Q108
rename V_109	Q109
rename V_110	Q110
rename V_111	Q111
rename V_112	Q112
rename V_113	Q113
rename V_114	Q114
rename V_115	Q115
rename V_116	Q116
rename V_117	Q117
rename V_118	Q118
rename V_119	Q119
rename V_120	Q120
rename V_121	Q121
rename V_122	Q122
rename V_123	Q123
rename V_124	Q124
rename V_125	Q125
rename V_126	Q126
rename V_127	Q127
rename V_127_A_1	Q127_A_1
rename V_127_A_2	Q127_A_2
rename V_127_A_3	Q127_A_3
rename V_128	Q128
rename V_129	Q129
rename V_130	Q130
rename V_131	Q131
rename V_132	Q132
rename V_133	Q133
rename V_134	Q134
rename V_135	Q135
rename V_136	Q136
rename V_137	Q137
rename V_138	Q138
rename V_139	Q139
rename V_140	Q140
rename V_141	Q141
rename V_142	Q142
rename V_143	Q143
rename V_144	Q144
rename V_145	Q145
rename V_146	Q146
rename V_147	Q147
rename V_148	Q148
rename V_149	Q149
rename V_150	Q150
rename V_151	Q151
rename V_152	Q152
rename V_153	Q153
rename V_154	Q154
rename V_155	Q155
rename V_156	Q156
rename V_156_A	Q156_A
rename V_157	Q157
rename V_157_A	Q157_A
rename V_158	Q158
rename V_159	Q159
rename V_160_A	Q160_A
rename V_160_B	Q160_B
rename V_160_C	Q160_C
rename V_160_D	Q160_D
rename V_161	Q161
rename V_162	Q162
rename V_163	Q163
rename V_164	Q164
rename V_165	Q165
rename V_166	Q166
rename V_167	Q167
rename V_168	Q168
rename V_169	Q169
rename V_170	Q170
rename V_171	Q171
rename V_172	Q172
rename V_173	Q173
rename V_174	Q174
rename V_174Ln	Q174Ln
rename V_175	Q175
rename V_176	Q176
rename V_177	Q177
rename V_178	Q178
rename V_179	Q179
rename V_179Ln	Q179Ln
rename V_180	Q180
rename V_180Ln	Q180Ln
rename V_181	Q181
rename V_182	Q182
rename V_183	Q183
rename V_184	Q184
rename V_185	Q185
rename V_185Ln	Q185Ln
rename V_186	Q186

rename *, upper

keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14	Q12	Q15 Q10	Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 2 if inlist(SE6, 5)
replace religion = 4 if inlist(SE6, 2, 3)
replace religion = 5 if inlist(SE6, 1)
replace religion = 6 if inlist(SE6, 7)
replace religion = 7 if inlist(SE6, 4)
replace religion = -99 if SE6==.

save "$data_dir/raw/Asia Barometer/W5 9. Indonesia/W5_Indonesia.dta", replace

///////////////////////////////////
//JAPAN
use "$data_dir/raw/Asia Barometer/W5 1. Japan/W5_Japan_merged_core_20220905_released.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13 Q14	Q12	Q15 Q10	Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(SE6, 9990)
replace religion = 2 if inlist(SE6, 60)
replace religion = 3 if inlist(SE6, 30)
replace religion = 4 if inlist(SE6, 10, 20)
replace religion = 5 if inlist(SE6, 40)
replace religion = 6 if inlist(SE6, 80, 75)
replace religion = -99 if SE6==.

save "$data_dir/raw/Asia Barometer/W5 1. Japan/W5_Japan.dta", replace

///////////////////////////////////
//SOUTH KOREA
use "$data_dir/raw/Asia Barometer/W5 3. South Korea/W5_Korea_merged_core_20210823_released.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(SE6, 9990)
replace religion = 2 if inlist(SE6, 60)
replace religion = 4 if inlist(SE6, 10, 20)
replace religion = 6 if inlist(SE6, 80)
replace religion = -99 if SE6==.

save "$data_dir/raw/Asia Barometer/W5 3. South Korea/W5_Korea.dta", replace

///////////////////////////////////
//TAIWAN
use "$data_dir/raw/Asia Barometer/W5 7. Taiwan/W5_Taiwan_coreQrelease_20190805.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W_FINAL Q99 Q132 Q1
rename W_FINAL W
destring COUNTRY, replace
label def taiwan 7 "Taiwan"
label val COUNTRY taiwan

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(SE6, 90)
replace religion = 2 if inlist(SE6, 60, 61)
replace religion = 3 if inlist(SE6, 30)
replace religion = 4 if inlist(SE6, 10, 20)
replace religion = 6 if inlist(SE6, 1, 76, 77, 80)
replace religion = -99 if inlist(SE6, 98, 99)

save "$data_dir/raw/Asia Barometer/W5 7. Taiwan/W5_Taiwan.dta", replace

///////////////////////////////////
//THAILAND
use "$data_dir/raw/Asia Barometer/W5 8. Thailand/W5_Thailand_merged_core_20210805_release.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 2 if inlist(SE6, 60)
replace religion = 4 if inlist(SE6, 10, 20)
replace religion = 5 if inlist(SE6, 40, 42)
replace religion = -99 if inlist(SE6, 99)

save "$data_dir/raw/Asia Barometer/W5 8. Thailand/W5_Thailand.dta", replace

///////////////////////////////////
//MALAYSIA
use "$data_dir/raw/Asia Barometer/W5 13. Malaysia/W5_Malaysia_coreQmerged_20210819_release.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 2 if inlist(SE6, 60)
replace religion = 4 if inlist(SE6, 10, 20)
replace religion = 5 if inlist(SE6, 40, 41)
replace religion = 6 if inlist(SE6, 70, 76, 77)
replace religion = 7 if inlist(SE6, 50)
replace religion = -99 if inlist(SE6, 99)

save "$data_dir/raw/Asia Barometer/W5 13. Malaysia/W5_Malaysia.dta", replace

///////////////////////////////////
//MYANMAR
use "$data_dir/raw/Asia Barometer/W5 14. Myanmar/W5_Myanmar_CoreQ_20220905_released.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL WEIGHT Q99 Q132 Q1
rename WEIGHT W

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 2 if inlist(SE6, 60)
replace religion = 4 if inlist(SE6, 10, 20)
replace religion = 5 if inlist(SE6, 40, 42)
replace religion = 6 if inlist(SE6, 80)
replace religion = 7 if inlist(SE6, 50)
replace religion = -99 if SE6==.

save "$data_dir/raw/Asia Barometer/W5 14. Myanmar/W5_Myanmar.dta", replace

///////////////////////////////////
//AUSTRALIA
use "$data_dir/raw/Asia Barometer/W5 15. Australia/W5_Australia_merged_core_20210803_release.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q23 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q92 Q125 Q1
rename Q92 Q99
rename Q125 Q132
rename Q23 Q22

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(SE6, 9990, 7200, 7201, 7202)
replace religion = 2 if inlist(SE6, 60)
replace religion = 3 if inlist(SE6, 5011)
replace religion = 4 if inlist(SE6, 10, 20, 21, 22, 23, 26, 81, 2000, 2010, 2110, 2151, 2171, 2232, 2233, 2234, 2236, 2237, 2239, 2251, 2271, 2331, 2402, 2808, 2904)
replace religion = 5 if inlist(SE6, 40)
replace religion = 6 if inlist(SE6, 70, 76, 6011, 6133, 6999, 7203, 7300, 7303)
replace religion = 7 if inlist(SE6, 50)
replace religion = -99 if inlist(SE6, -1, 9999)

save "$data_dir/raw/Asia Barometer/W5 15. Australia/W5_Australia.dta", replace

///////////////////////////////////
//INDIA
use "$data_dir/raw/Asia Barometer/W5 16. India/W5_India_merged_core_20220905_released.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(SE6, 9990)
replace religion = 2 if inlist(SE6, 60)
replace religion = 4 if inlist(SE6, 2000)
replace religion = 5 if inlist(SE6, 40)
replace religion = 6 if inlist(SE6, 66, 71, 80)
replace religion = 7 if inlist(SE6, 50)
replace religion = -99 if SE6==.

save "$data_dir/raw/Asia Barometer/W5 16. India/W5_India.dta", replace

///////////////////////////////////
//MONGOLIA
use "$data_dir/raw/Asia Barometer/W5_5_Mongolia/ABS_V_Mongolia_merged_core_20201217_release.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(SE6, 90)
replace religion = 2 if inlist(SE6, 60)
replace religion = 4 if inlist(SE6, 10, 20)
replace religion = 5 if inlist(SE6, 40)
replace religion = 6 if inlist(SE6, 80)
replace religion = -99 if SE6==.

save "$data_dir/raw/Asia Barometer/W5_5_Mongolia/ABS_V_Mongolia.dta", replace

///////////////////////////////////
//PHILIPPINES
use "$data_dir/raw/Asia Barometer/W5_6_ThePhilippines/ABS Wave 5 Philippines_Core_merged_20201223_release.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 4 if inlist(SE6, 10, 20, 72, 73, 74)
replace religion = 5 if inlist(SE6, 40)
replace religion = 6 if inlist(SE6, 70, 75)
replace religion = -99 if inlist(SE6, 99)

save "$data_dir/raw/Asia Barometer/W5_6_ThePhilippines/ABS Wave 5 Philippines.dta", replace

///////////////////////////////////
//VIETNAM
use "$data_dir/raw/Asia Barometer/W5_11_Vietnam/W5_Vietnam_merged_core_20201215_release.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q99 Q132 Q1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 2 if inlist(SE6, 60)
replace religion = 4 if inlist(SE6, 10, 20)
replace religion = 5 if inlist(SE6, 40)
replace religion = 6 if inlist(SE6, 201, 202)
replace religion = -99 if inlist(SE6, 9999)

save "$data_dir/raw/Asia Barometer/W5_11_Vietnam/W5_Vietnam.dta", replace


//APPEND ALL DATASETS
clear
use "$data_dir/raw/Asia Barometer/W5 9. Indonesia/W5_Indonesia.dta"
append using "$data_dir/raw/Asia Barometer/W5 1. Japan/W5_Japan.dta"
append using "$data_dir/raw/Asia Barometer/W5 3. South Korea/W5_Korea.dta"
append using "$data_dir/raw/Asia Barometer/W5 7. Taiwan/W5_Taiwan.dta"
append using "$data_dir/raw/Asia Barometer/W5 8. Thailand/W5_Thailand.dta"
append using "$data_dir/raw/Asia Barometer/W5 13. Malaysia/W5_Malaysia.dta"
append using "$data_dir/raw/Asia Barometer/W5 14. Myanmar/W5_Myanmar.dta"
append using "$data_dir/raw/Asia Barometer/W5 15. Australia/W5_Australia.dta"
append using "$data_dir/raw/Asia Barometer/W5 16. India/W5_India.dta"
append using "$data_dir/raw/Asia Barometer/W5_5_Mongolia/ABS_V_Mongolia.dta"
append using "$data_dir/raw/Asia Barometer/W5_6_ThePhilippines/ABS Wave 5 Philippines.dta"
append using "$data_dir/raw/Asia Barometer/W5_11_Vietnam/W5_Vietnam.dta"

decode COUNTRY, gen(country)
replace country = "Taiwan, Province of China" if country=="Taiwan"
replace country = "Korea, Republic of" if country=="Korea"
replace country = "Viet Nam" if country=="Vietnam"
replace country = "India" if COUNTRY==18

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Asiabarometer"
gen wave = 5

gen weight = W

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if Q9==1|Q9==2|Q9==3
replace trust_govt = 0 if Q9==4|Q9==5|Q9==6

gen trust_justice = 1 if Q8==1|Q8==2|Q8==3
replace trust_justice = 0 if Q8==4|Q8==5|Q8==6

gen trust_police = 1 if Q14==1|Q14==2|Q14==3
replace trust_police = 0 if Q14==4|Q14==5|Q14==6

gen trust_civil_service = 1 if Q12==1|Q12==2|Q12==3
replace trust_civil_service = 0 if Q12==4|Q12==5|Q12==6

gen trust_local_govt = 1 if Q15==1|Q15==2|Q15==3
replace trust_local_govt = 0 if Q15==4|Q15==5|Q15==6

gen trust_parties = 1 if Q10==1|Q10==2|Q10==3
replace trust_parties = 0 if Q10==4|Q10==5|Q10==6

gen trust_parliament = 1 if Q11==1|Q11==2|Q11==3
replace trust_parties = 0 if Q11==4|Q11==5|Q11==6

gen trust_others = 1 if Q22==1
replace trust_others = 0 if Q22==2

gen trust_military = 1 if Q13==1|Q13==2|Q13==3
replace trust_military = 0 if Q13==4|Q13==5|Q13==6

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = YEAR

clonevar yob = SE3
replace yob = . if yob==-1|yob==999|yob==9999

gen age = survey_year - yob

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if SE2==1
replace sex = 2 if SE2==2
replace sex = -99 if SE2==.|SE2==-1|SE2==3

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if SE5==2|SE5==3
replace edu = 2 if SE5==4|SE5==5|SE5==6|SE5==7
replace edu = 3 if SE5==8|SE5==9|SE5==10
replace edu = 4 if SE5==1
replace edu = -99 if SE5==.|SE5==99|SE5==-1|SE5==98|SE5==11

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

tostring SE9C, gen(isco)
replace isco = "" if isco=="-2"|isco=="-1"|isco=="0"|isco=="9990"|isco=="9998"|isco=="9999"
gen occupation = regexs(0) if regex(isco, "[0-9]")
replace occupation = "-99" if occupation==""
destring occupation, replace

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if SE9==1
replace employment_status = 2 if SE9==2
replace employment_status = -99 if SE9==-1|SE9==9|SE9==.|SE9==8

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = .
replace income = 1 if SE14==1
replace income = 3 if SE14==2
replace income = 5 if SE14==3
replace income = 7 if SE14==4
replace income = 9 if SE14==5
replace income = -99 if inlist(SE14, -1, 8, 9)

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if inlist(SE4, 2, 3)
replace marital = 2 if inlist(SE4, 1, 4, 5, 6)
replace marital = -99 if inlist(SE4, 9)
replace marital = -99 if SE4==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if LEVEL==2
replace urban = 2 if LEVEL==1
replace urban = -99 if LEVEL==.|LEVEL==-1

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/asia5_clean.dta", replace

***************************************************************************************************************

*Asiabarometer 6

///////////////////////////////////
//INDONESIA
use "$data_dir/raw/Asia Barometer/W6_9_Indonesia/W6_Indonesia_release_20240402.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14	Q12	Q15 Q10	Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_9_Indonesia/W6_Indonesia.dta", replace

///////////////////////////////////
//SOUTH KOREA
use "$data_dir/raw/Asia Barometer/W6_3_Korea/W6_Korea_Release_20241220.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_3_Korea/W6_Korea.dta", replace

///////////////////////////////////
//TAIWAN
use "$data_dir/raw/Asia Barometer/W6_7_Taiwan/W6_Taiwan_EN_20240402.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_7_Taiwan/W6_Taiwan.dta", replace

///////////////////////////////////
//THAILAND
use "$data_dir/raw/Asia Barometer/W6_8_Thailand/W6_8_Thailand_Release_20250108.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_8_Thailand/W6_Thailand.dta", replace

///////////////////////////////////
//AUSTRALIA
use "$data_dir/raw/Asia Barometer/W6_15_Australia/W6_15_Australia_Release_20250305.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_15_Australia/W6_Australia.dta", replace

///////////////////////////////////
//MONGOLIA
use "$data_dir/raw/Asia Barometer/W6_5_Mongolia/W6_Mongolia_Release_20241223.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_5_Mongolia/W6_Mongolia.dta", replace

///////////////////////////////////
//PHILIPPINES
use "$data_dir/raw/Asia Barometer/W6_6_Philippines/W6_Philippines_release_20240403.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_6_Philippines/W6_Philippines.dta", replace

///////////////////////////////////
//VIETNAM
use "$data_dir/raw/Asia Barometer/W6_11_Vietnam/W6_11_Vietnam_Release_20250117.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_11_Vietnam/W6_Vietnam.dta", replace

///////////////////////////////////
//CAMBODIA
use "$data_dir/raw/Asia Barometer/W6_12_Cambodia_20240819/W6_Cambodia_Release_20240819.dta", clear
rename *, upper
keep COUNTRY YEAR Q9 Q8 Q7 Q13	Q14 Q12 Q15 Q10 Q11	Q22 SE3 SE2 SE6 SE5 SE9C SE9 SE14 SE4 LEVEL W Q90 Q128 Q1

save "$data_dir/raw/Asia Barometer/W6_12_Cambodia_20240819/W6_Cambodia.dta", replace

//APPEND ALL DATASETS
clear
use "$data_dir/raw/Asia Barometer/W6_9_Indonesia/W6_Indonesia.dta"
append using "$data_dir/raw/Asia Barometer/W6_3_Korea/W6_Korea.dta"
append using "$data_dir/raw/Asia Barometer/W6_7_Taiwan/W6_Taiwan.dta"
append using "$data_dir/raw/Asia Barometer/W6_8_Thailand/W6_Thailand.dta"
append using "$data_dir/raw/Asia Barometer/W6_15_Australia/W6_Australia.dta"
append using "$data_dir/raw/Asia Barometer/W6_5_Mongolia/W6_Mongolia.dta"
append using "$data_dir/raw/Asia Barometer/W6_6_Philippines/W6_Philippines.dta"
append using "$data_dir/raw/Asia Barometer/W6_11_Vietnam/W6_Vietnam.dta"
append using "$data_dir/raw/Asia Barometer/W6_12_Cambodia_20240819/W6_Cambodia.dta"

decode COUNTRY, gen(country)
replace country = "Taiwan, Province of China" if country=="Taiwan"
replace country = "Korea, Republic of" if country=="Korea"
replace country = "Viet Nam" if country=="Vietnam"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Asiabarometer"
gen wave = 6

gen weight = W

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if inlist(Q9, 1, 2)
replace trust_govt = 0 if inlist(Q9, 3, 4)

gen trust_justice = 1 if inlist(Q8, 1, 2)
replace trust_justice = 0 if inlist(Q8, 3, 4)

gen trust_police = 1 if inlist(Q14, 1, 2)
replace trust_police = 0 if inlist(Q14, 3, 4)

gen trust_civil_service = 1 if inlist(Q12, 1, 2)
replace trust_civil_service = 0 if inlist(Q12, 3, 4)

gen trust_local_govt = 1 if inlist(Q15, 1, 2)
replace trust_local_govt = 0 if inlist(Q15, 3, 4)

gen trust_parties = 1 if inlist(Q10, 1, 2)
replace trust_parties = 0 if inlist(Q10, 3, 4)

gen trust_parliament = 1 if inlist(Q11, 1, 2)
replace trust_parties = 0 if inlist(Q11, 3, 4)

gen trust_others = 1 if Q22==1
replace trust_others = 0 if Q22==2

gen trust_military = 1 if inlist(Q13, 1, 2)
replace trust_military = 0 if inlist(Q13, 3, 4)

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = YEAR

clonevar yob = SE3

gen age = survey_year - yob

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if SE2==1
replace sex = 2 if SE2==2
replace sex = -99 if SE2==.|SE2==99

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(SE6, 99)
replace religion = 2 if inlist(SE6, 60)
replace religion = 3 if inlist(SE6, 30)
replace religion = 4 if inlist(SE6, 10, 20, 72, 73, 74)
replace religion = 5 if inlist(SE6, 40, 41, 42)
replace religion = 7 if inlist(SE6, 50, 71)
replace religion = -99 if inlist(SE6, 996, 998, 999, -1)
replace religion = 6 if religion==. & SE6!=.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if SE5==2|SE5==3
replace edu = 2 if SE5==4|SE5==5|SE5==6|SE5==7
replace edu = 3 if SE5==8|SE5==9|SE5==10
replace edu = 4 if SE5==1
replace edu = -99 if SE5==.|SE5==99|SE5==-1|SE5==98|SE5==11

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if SE9C==2|SE9C==10
replace occupation = 2 if SE9C==1
replace occupation = 4 if SE9C==3
replace occupation = 5 if SE9C==4|SE9C==5
replace occupation = 6 if SE9C==9
replace occupation = 7 if SE9C==6
replace occupation = 8 if SE9C==7
replace occupation = 9 if SE9C==8
replace occupation = -99 if inlist(SE9C, -1, 0 , 12, 13)

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if SE9==1
replace employment_status = 2 if SE9==2
replace employment_status = -99 if SE9==-1|SE9==9|SE9==.|SE9==8

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = .
replace income = 1 if SE14==1
replace income = 3 if SE14==2
replace income = 5 if SE14==3
replace income = 7 if SE14==4
replace income = 9 if SE14==5
replace income = -99 if inlist(SE14, -1, 8, 9)

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if inlist(SE4, 2, 3)
replace marital = 2 if inlist(SE4, 1, 4, 5, 6)
replace marital = -99 if inlist(SE4, 9)
replace marital = -99 if SE4==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if LEVEL==2
replace urban = 2 if LEVEL==1
replace urban = -99 if LEVEL==.|LEVEL==-1

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/asia6_clean.dta", replace
